package com.luo.controller;

import com.luo.pojo.Emp;
import com.luo.pojo.PageBean;
import com.luo.pojo.Results;
import com.luo.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Results getEmp(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          String name, Short gender,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询员工信息{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
        PageBean pageBean = empService.getEmp(page, pageSize, name, gender, begin, end);
        return Results.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Results deleteEmp(@PathVariable List<Integer> ids) {
        log.info("删除员工信息{}", ids);
        empService.deleteEmp(ids);
        return Results.success();
    }

    @PostMapping
    public Results addEmp(@RequestBody Emp emp) {
        log.info("新增员工信息{}", emp);
        empService.addEmp(emp);
        return Results.success();
    }

    @GetMapping("/{id}")
    public Results getEmpById(@PathVariable Integer id) {
        log.info("根据id查询员工信息{}", id);
        Emp emp = empService.getEmpById(id);
        return Results.success(emp);
    }

    @PutMapping
    public Results updateEmp(@RequestBody Emp emp) {
        log.info("修改员工信息{}", emp);
        empService.updateEmp(emp);
        return Results.success();
    }


}
